<%@page trimDirectiveWhitespaces="true"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="com.captcha.botdetect.web.CaptchaUrls"%>
<%@ taglib uri="https://captcha.com/java/jsp" prefix="botDetect" %>
<%@ include file="/commons/taglibs.jsp" %>
<html>
	<head>
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
		<meta charset="utf-8" />
		<title>Login Page - Ace Admin</title>

		<meta name="description" content="User login page" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />

		<!-- bootstrap & fontawesome -->
		<link rel="stylesheet" href="${ctx}/assets/css/bootstrap.min.css" />
		<link rel="stylesheet" href="${ctx}/assets/font-awesome/4.5.0/css/font-awesome.min.css" />

		<!-- text fonts -->
		<link rel="stylesheet" href="${ctx}/assets/css/fonts.googleapis.com.css" />

		<!-- ace styles -->
		<link rel="stylesheet" href="${ctx}/assets/css/ace.min.css" />

		<!--[if lte IE 9]>
			<link rel="stylesheet" href="${ctx}/assets/css/ace-part2.min.css" />
		<![endif]-->
		<link rel="stylesheet" href="${ctx}/assets/css/ace-rtl.min.css" />

		<!--[if lte IE 9]>
		  <link rel="stylesheet" href="${ctx}/assets/css/ace-ie.min.css" />
		<![endif]-->

		<!-- HTML5shiv and Respond.js for IE8 to support HTML5 elements and media queries -->

		<!--[if lte IE 8]>
		<script src="${ctx}/assets/js/html5shiv.min.js"></script>
		<script src="${ctx}/assets/js/respond.min.js"></script>
		<![endif]-->
		<!-- basic scripts -->

		<!--[if !IE]> -->
		<script src="${ctx}/assets/js/jquery-2.1.4.min.js"></script>
	
		<c:set value="${ctx }/encryption/generateKeypair?generateKeypair=true" var="getKeysURL"></c:set>
		 
		<!-- <![endif]-->

		<!--[if IE]>
		<script src="${ctx}/assets/js/jquery-1.11.3.min.js"></script>
		<![endif]-->

		<script type="text/javascript" src="${ctx }/assets/js/e2e/aes.js"></script>
 		<script type="text/javascript" src="${ctx }/assets/js/e2e/base64.js"></script>
		<script type="text/javascript" src="${ctx }/assets/js/e2e/e2e.js"></script>
		<script type="text/javascript" src="${ctx }/assets/js/e2e/jsbn.js"></script>
		<script type="text/javascript" src="${ctx }/assets/js/e2e/prng4.js"></script>
		<script type="text/javascript" src="${ctx }/assets/js/e2e/rng.js"></script>
		<script type="text/javascript" src="${ctx }/assets/js/e2e/rsa.js"></script>
		<script src="${ctx}/assets/js/bootstrap-datepicker.min.js"></script>
		<script src="${ctx}/assets/js/validation/jquery.validate.min.js"></script>
		<script src="${ctx}/assets/js/validation/jquery.metadata.js"></script>
		<script type="text/javascript">
		if("${sessionScope['org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE']}" != 'en_US')
		{
			document.write("<script src='${ctx}/assets/js/validation/messages_zh.js'>"+"<" + "/script>");
		}
		</script>
		<script type="text/javascript" src="${ctx }/assets/js/myjs/app.js"></script>
 		
		<script type="text/javascript">
			if('ontouchstart' in document.documentElement) document.write("<script src='${ctx}/assets/js/jquery.mobile.custom.min.js'>"+"<"+"/script>");
		</script>
		<script type="text/javascript" src="${ctx }/<%=CaptchaUrls.getScriptIncludeUrl() %>"></script>
	</head>

	<body class="login-layout">
		<div class="main-container">
			<div class="main-content">
				<div class="row">
					<div class="col-sm-10 col-sm-offset-1">
						<div class="login-container">
							<div class="center">
								<h1>
									<i class="${systemConfig.systemLogo }"></i>
									<span class="red"></span>
									<span class="white" id="id-text2">${systemConfig.systemName }</span>
								</h1>
								<h4 class="blue" id="id-company-text">&copy; ${systemConfig.companyName }</h4>
							</div>

							<div class="space-6"></div>

							<div class="position-relative">
								<div id="login-box" class="login-box visible widget-box no-border">
									<div class="widget-body">
										<div class="widget-main">
											<h4 class="header blue lighter bigger">
												<i class="ace-icon fa fa-coffee green"></i>
												Please Enter Your Information
											</h4>

											<div class="space-6"></div>
											<input type="hidden" name="redirectURL" value="${redirectURL}" /> 
											<form action="${ctx}/login" method="post" id="loginForm">
												<fieldset>		
													<div class="form-group">																
															<div class="col-md-12 input-group">
																<input type="text" class="form-control required" placeholder="Username" id="username" name="username"/>
																<span class="input-group-addon"><span class="ace-icon fa fa-user"></span></span>
															</div>
													</div>
													<div class="form-group">																
															<div class="col-md-12 input-group">
																<input type="password" class="form-control required" placeholder="Password"  id="password" name="password"/>
																<span class="input-group-addon"><span class="ace-icon fa fa-lock"></span></span>
															</div>
													</div>
													<div class="form-group">		
															<botDetect:captcha id="loginCaptcha" 
											                userInputID="captchaCode"
											                codeLength="4"
											                imageWidth="200"
											                codeStyle="ALPHA" />														
															<div class="col-md-12 input-group">
																<input id="captchaCode" type="text" name="captchaCode" class="form-control required"/>
																<span class="input-group-addon"><span class="ace-icon fa fa-lock"></span></span>
															</div>
													</div>
													
													<div class="space"></div>

													<div class="clearfix">
														<label class="inline">
															<input type="checkbox" class="ace" />
															<span class="lbl"> Remember Me</span>
														</label>

														<button type="submit" class="width-35 pull-right btn btn-sm btn-primary" id="login-btn">
															<i class="ace-icon fa fa-key"></i>
															<span class="bigger-110">Login</span>
														</button>
													</div>

													<div class="space-4"></div>
												</fieldset>
											</form>

										</div><!-- /.widget-main -->

										
									</div><!-- /.widget-body -->
								</div><!-- /.login-box -->

								<div id="forgot-box" class="forgot-box widget-box no-border">
									<div class="widget-body">
										<div class="widget-main">
											<h4 class="header red lighter bigger">
												<i class="ace-icon fa fa-key"></i>
												Retrieve Password
											</h4>

											<div class="space-6"></div>
											<p>
												Enter your email and to receive instructions
											</p>

											<form>
												<fieldset>
													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input type="email" class="form-control" placeholder="Email" />
															<i class="ace-icon fa fa-envelope"></i>
														</span>
													</label>

													<div class="clearfix">
														<button type="button" class="width-35 pull-right btn btn-sm btn-danger">
															<i class="ace-icon fa fa-lightbulb-o"></i>
															<span class="bigger-110">Send Me!</span>
														</button>
													</div>
												</fieldset>
											</form>
										</div><!-- /.widget-main -->

										<div class="toolbar center">
											<a href="#" data-target="#login-box" class="back-to-login-link">
												Back to login
												<i class="ace-icon fa fa-arrow-right"></i>
											</a>
										</div>
									</div><!-- /.widget-body -->
								</div><!-- /.forgot-box -->

								<div id="signup-box" class="signup-box widget-box no-border">
									<div class="widget-body">
										<div class="widget-main">
											<h4 class="header green lighter bigger">
												<i class="ace-icon fa fa-users blue"></i>
												New User Registration
											</h4>

											<div class="space-6"></div>
											<p> Enter your details to begin: </p>

											<form>
												<fieldset>
													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input type="email" class="form-control" placeholder="Email" />
															<i class="ace-icon fa fa-envelope"></i>
														</span>
													</label>

													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input type="text" class="form-control" placeholder="Username" />
															<i class="ace-icon fa fa-user"></i>
														</span>
													</label>

													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input type="password" class="form-control" placeholder="Password" />															
															<i class="ace-icon fa fa-lock"></i>
														</span>
													</label>
																		
													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input type="password" class="form-control" placeholder="Repeat password" />
															<i class="ace-icon fa fa-retweet"></i>
														</span>
													</label>

													<label class="block">
														<input type="checkbox" class="ace" />
														<span class="lbl">
															I accept the
															<a href="#">User Agreement</a>
														</span>
													</label>

													<div class="space-24"></div>

													<div class="clearfix">
														<button type="reset" class="width-30 pull-left btn btn-sm">
															<i class="ace-icon fa fa-refresh"></i>
															<span class="bigger-110">Reset</span>
														</button>

														<button type="button" class="width-65 pull-right btn btn-sm btn-success">
															<span class="bigger-110">Register</span>

															<i class="ace-icon fa fa-arrow-right icon-on-right"></i>
														</button>
													</div>
												</fieldset>
											</form>
										</div>

										<div class="toolbar center">
											<a href="#" data-target="#login-box" class="back-to-login-link">
												<i class="ace-icon fa fa-arrow-left"></i>
												Back to login
											</a>
										</div>
									</div><!-- /.widget-body -->
								</div><!-- /.signup-box -->
							</div><!-- /.position-relative -->

							<div class="navbar-fixed-top align-right">
								<br />
								&nbsp;
								<a id="btn-login-dark" href="#">Dark</a>
								&nbsp;
								<span class="blue">/</span>
								&nbsp;
								<a id="btn-login-blur" href="#">Blur</a>
								&nbsp;
								<span class="blue">/</span>
								&nbsp;
								<a id="btn-login-light" href="#">Light</a>
								&nbsp; &nbsp; &nbsp;
							</div>
						</div>
					</div><!-- /.col -->
				</div><!-- /.row -->
			</div><!-- /.main-content -->
		</div><!-- /.main-container -->


		<!-- inline scripts related to this page -->
		<script type="text/javascript">
			${rsascript}
			jQuery(function($) {
			 $(document).on('click', '.toolbar a[data-target]', function(e) {
				e.preventDefault();
				var target = $(this).data('target');
				$('.widget-box.visible').removeClass('visible');//hide others
				$(target).addClass('visible');//show target
			 });
			});
			
			
			
			//you don't need this, just used for changing background
			jQuery(function($) {
			 $('#btn-login-dark').on('click', function(e) {
				$('body').attr('class', 'login-layout');
				$('#id-text2').attr('class', 'white');
				$('#id-company-text').attr('class', 'blue');
				
				e.preventDefault();
			 });
			 $('#btn-login-light').on('click', function(e) {
				$('body').attr('class', 'login-layout light-login');
				$('#id-text2').attr('class', 'grey');
				$('#id-company-text').attr('class', 'blue');
				
				e.preventDefault();
			 });
			 $('#btn-login-blur').on('click', function(e) {
				$('body').attr('class', 'login-layout blur-login');
				$('#id-text2').attr('class', 'white');
				$('#id-company-text').attr('class', 'light-blue');
				
				e.preventDefault();
			 });
			 
			 
			
			loginformvalidation("#loginForm",{
				errorElement: 'div',
				errorClass: 'help-block',
				rules:{
					captchaCode:{
						remote:$("#captchaCode").get(0).Captcha.ValidationUrl
					}
				},
				messages:{
					captchaCode:{
						remote:"The Captcha code must be retyped correctly"
					}
				},
				onkeyup: false,
				showErrors: function(errorMap, errorList) {
			      if (typeof(errorMap.captchaCode) != "undefined" &&
			          errorMap.captchaCode === this.settings.messages.captchaCode.remote) {
			        $("#captchaCode").get(0).Captcha.ReloadImage();
			      }
			      this.defaultShowErrors();
			    },
				errorPlacement: function (error, element) {
					error.insertAfter(element.parent());
				},
				submitHandler:function(form){
					$.ajax({
						 url:"/backend/encryption/generateKeypair?generateKeypair=true",
						 success:function(data){
							 var rsa = new RSAKey();
							 rsa.setPublic(data.n,data.e);
							 var aes = new AES(128);
							 
							 var encryptedAesKey = rsa.encrypt(aes.key);
							 
							 var serialize = $("#loginForm").serialize();
							 var encrypt = aes.encrypt(serialize);
							 
							 $encryptedElement = $("<input />",{
							        type:'hidden',
							        name:"jCryption"
							      });
							 $encryptedElement.val(encryptedAesKey+"|"+encrypt);
							 
							 $.ajax({
								 url:"/backend/encryption/handshake",
								 data:{
									 key:encryptedAesKey
								 },
								 success:function(data){
									 if(aes.key == aes.decrypt(data.challenge)){
										 $("#loginForm").find(":input").attr("disabled",true).end().append($encryptedElement);
										 form.submit();
									 }else{
										 if(confirm("Authentication with Server failed, are you sure you want to submit this form unencrypted?"))
										{
											 form.submit();
										}
									 }						 
								 },
								 error:function(){
									 confirm("Authentication with Server failed, are you sure you want to submit this form unencrypted?",function(){
										 form.submit();
									 })
								 }					 
							 })
							 
							
						 }
					 })
				}
			});
			
			
			//secureSubmit("#loginForm","#login-btn");
			});
		</script>
	</body>
</html>
